package com.nofeng.wx.third;

import com.nofeng.wx.utils.NoHttpsClientUtils;
import com.zjiecode.wxpusher.client.WxPusher;
import com.zjiecode.wxpusher.client.bean.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class WeixinApi {

    public void qyapiWebhook(){
        try {
            String api = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=efff888d-f332-4c66-8779-3ccb7d9f7885";
            String textMsg = "{\"msgtype\":\"text\",\"text\":{\"content\":\"你好，我是机器人\"},\"at\":{\"atMobiles\":[\"xxx\"],\"isAtAll\":false}}";

            String result = NoHttpsClientUtils.getRestTemplate().postForObject(api,textMsg,String.class);
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public String get(String pach){
        try {
            return NoHttpsClientUtils.getRestTemplate().getForObject(pach,String.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "{}";
    }

    public void push(String msg){
        String appToken = "AT_1pP43f5vYq9TyJnZaq8u5IwvVqs0JhyB";
        Result<Page<WxUser>> wxUserList = WxPusher.queryWxUser(appToken,1,50);
        for (WxUser record : wxUserList.getData().getRecords()) {
            String uId=record.getUid();
            Message message = new Message();
            message.setAppToken(appToken);
            message.setContentType(Message.CONTENT_TYPE_TEXT);
            message.setContent(msg);
            message.setUid(uId);
            message.setUrl("http://8.138.24.62:8080/onhand.html");//可选参数
            WxPusher.send(message);
        }
    }
}
